<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Exception\Handler;

use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;
use Throwable;

class ValidationExceptionHandler extends \Hyperf\Validation\ValidationExceptionHandler
{
    public function handle(Throwable $throwable, ResponseInterface $response)
    {
        $this->stopPropagation();
        /** @var \Hyperf\Validation\ValidationException $throwable */
//        $body = $throwable->validator->errors()->first();

        $messageList = [];
        foreach ($throwable->validator->errors()->all() as $error) {
            $messageList[] = $error;
        }
        $message = implode('<br>', $messageList);
        $format = json_encode(['message' => $message]);


        return $response->withStatus(200)->withBody(new SwooleStream($format));
    }
}
